package com.thh.datamonitor.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.thh.common.exception.ServiceException;
import com.thh.common.utils.DateUtils;
import com.thh.common.utils.ShiroUtils;
import com.thh.datamonitor.dto.TmdUserDatabaseDto;
import com.thh.datamonitor.dto.TmdUserDatabaseQueryDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.thh.datamonitor.param.mapper.TmdUserDatabaseMapper;
import com.thh.datamonitor.entity.TmdUserDatabase;
import com.thh.datamonitor.service.ITmdUserDatabaseService;
import com.thh.common.core.text.Convert;

/**
 * 用户和数据库绑定Service业务层处理
 * 
 * @author thh
 * @date 2022-06-23
 */
@Service
public class TmdUserDatabaseServiceImpl implements ITmdUserDatabaseService 
{
    @Autowired
    private TmdUserDatabaseMapper tmdUserDatabaseMapper;

    /**
     * 查询用户和数据库绑定
     * 
     * @param userDatabaseId 用户和数据库绑定主键
     * @return 用户和数据库绑定
     */
    @Override
    public TmdUserDatabase selectTmdUserDatabaseByUserDatabaseId(Long userDatabaseId)
    {
        return tmdUserDatabaseMapper.selectTmdUserDatabaseByUserDatabaseId(userDatabaseId);
    }

    /**
     * 查询用户和数据库绑定列表
     *
     * @param tmdUserDatabaseQueryDto 用户和数据库绑定
     * @return 用户和数据库绑定
     */
    @Override
    public List<TmdUserDatabaseDto> selectTmdUserDatabaseList(TmdUserDatabaseQueryDto tmdUserDatabaseQueryDto)
    {
        return tmdUserDatabaseMapper.selectTmdUserDatabaseList(tmdUserDatabaseQueryDto);
    }

    /**
     * 新增用户和数据库绑定
     * 
     * @param tmdUserDatabase 用户和数据库绑定
     * @return 结果
     */
    @Override
    public int insertTmdUserDatabase(TmdUserDatabase tmdUserDatabase)
    {
        QueryWrapper<TmdUserDatabase> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("database_id", tmdUserDatabase.getDatabaseId());
        queryWrapper.eq("user_id", tmdUserDatabase.getUserId());
        Long count = tmdUserDatabaseMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new ServiceException("该用户已有该数据库配置，请勿重复配置！");
        }
        tmdUserDatabase.setCreateBy(ShiroUtils.getLoginName());
        tmdUserDatabase.setCreateTime(DateUtils.getNowDate());
        return tmdUserDatabaseMapper.insert(tmdUserDatabase);
    }

    /**
     * 修改用户和数据库绑定
     * 
     * @param tmdUserDatabase 用户和数据库绑定
     * @return 结果
     */
    @Override
    public int updateTmdUserDatabase(TmdUserDatabase tmdUserDatabase)
    {
        QueryWrapper<TmdUserDatabase> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("database_id", tmdUserDatabase.getDatabaseId());
        queryWrapper.eq("user_id", tmdUserDatabase.getUserId());
        queryWrapper.notIn("user_database_id", tmdUserDatabase.getUserDatabaseId());
        queryWrapper.notIn("status", "-1");
        Long count = tmdUserDatabaseMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new ServiceException("该用户已有该数据库配置，请勿重复配置！");
        }
        tmdUserDatabase.setUpdateTime(DateUtils.getNowDate());
        tmdUserDatabase.setUpdateBy(ShiroUtils.getLoginName());
        return tmdUserDatabaseMapper.updateById(tmdUserDatabase);
    }

    /**
     * 批量删除用户和数据库绑定
     * 
     * @param userDatabaseIds 需要删除的用户和数据库绑定主键
     * @return 结果
     */
    @Override
    public int deleteTmdUserDatabaseByUserDatabaseIds(String userDatabaseIds)
    {
        return tmdUserDatabaseMapper.deleteUserDatabaseByIds(Convert.toStrArray(userDatabaseIds));
    }

    /**
     * 删除用户和数据库绑定信息
     * 
     * @param userDatabaseId 用户和数据库绑定主键
     * @return 结果
     */
    @Override
    public int deleteTmdUserDatabaseByUserDatabaseId(Long userDatabaseId)
    {
        return tmdUserDatabaseMapper.deleteById(userDatabaseId);
    }

    @Override
    public List<TmdUserDatabaseDto> selectMyUserDatabaseList(TmdUserDatabaseQueryDto tmdUserDatabaseQueryDto) {
        return tmdUserDatabaseMapper.selectMyUserDatabaseList(tmdUserDatabaseQueryDto);
    }
}
